﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System;

namespace ConfigGen.Core.ConfigurationSettings
{
    /// <summary>
    /// Represents a single configuration setting, i.e. the name of the setting (the 'token') and its value. This class is immutable.
    /// </summary>
    [Serializable]
    public class ConfigurationSetting
    {
        private readonly string _token;
        private readonly string _value;
        private readonly MachineConfigurationSettings _machine;

        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigurationSetting"/> class.
        /// </summary>
        /// <param name="token">The token.</param>
        /// <param name="value">The value.</param>
        /// <param name="machine">The machine to which this instance belongs</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="token"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="token"/> is zero length.</exception>
        public ConfigurationSetting(string token, string value, MachineConfigurationSettings machine = null)
        {
            if (token == null) throw new ArgumentNullException("token");
            if (token.Length == 0) throw new ArgumentException("token cannot be zero length");

            _token = token;
            _value = value;
            _machine = machine;
        }

        /// <summary>
        /// Gets the token name.
        /// </summary>
        /// <value>The token.</value>
        public string Token
        {
            get { return _token; }
        }

        /// <summary>
        /// Gets the value of the cell.
        /// </summary>
        /// <value>The value.</value>
        public string Value
        {
            get
            {
                return _value;
            }
        }

        /// <summary>
        /// Gets the machine configuration settings instance to which this setting belongs.
        /// </summary>
        public MachineConfigurationSettings ParentMachineConfigurationSettings
        {
            get { return _machine;  }
        }

        /// <summary>
        /// Returns a human-readable representation of the current instance.
        /// </summary>
        /// <returns>a human-readable representation of the current instance</returns>
        public override string ToString()
        {
            return string.Format("{0}: Token={1}, Value={2}", typeof(ConfigurationSetting).Name, Token, Value ?? "(null)");
        }
    }
}
